/**
 * @author ThanhNgo
 * @date Dec 10, 2013
 *
 */
package com.bestbuy.daoimple;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bestbuy.dao.AccountDao;
import com.bestbuy.pojo.Account;
import com.bestbuy.pojo.AccountInfo;
import com.bestbuy.pojo.AccountRole;
import com.bestbuy.pojo.Cart;
import com.bestbuy.util.AccountUtil;

/**
 * @author ThanhNgo
 * 
 */

@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class AccountDaoImpl implements AccountDao {

	@Autowired
	private SessionFactory sessionFactory;

	public AccountInfo getInfoAccount(Account account) {
		return null;
	}

	public AccountRole getRoleAccount(String role) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from AccountRole where role = ?";
		Query query = session.createQuery(hql);
		query.setString(0, role);
		AccountRole accountRole = (AccountRole) query.uniqueResult();
		return accountRole;
	}

	/*
	 * author ThanhNgo
	 */
	public List<Account> getListAccount() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Account";
		Query query = session.createQuery(hql);
		return query.list();
	}

	

	/*
	 * author ThanhNgo
	 */
	public Account RegisterAccount(String username, String password) {
		Session session = sessionFactory.getCurrentSession();
		// kiem tra xem user nay co ton tai chua
		String hql = "select p from Account p where p.username = ?";
		Query query = session.createQuery(hql);
		query.setString(0, username);
		Account account = (Account) query.uniqueResult();
		if (account != null) // user nay` da ton tai
		{
			return null;
		} else {
			// khoi tao account
			// luu account lai
			hql = "from AccountRole where role = ?";
			query = session.createQuery(hql);
			query.setString(0, "User");
			AccountRole role = (AccountRole) query.uniqueResult();
			Account acc = new Account();
			acc.setUsername(username);
			acc.setPassword(password);
			acc.setIsdeleted(0);
			acc.setAccountRole(role);
			role.getAccounts().add(acc);
			// tao info cho account nay
			AccountInfo accInfo = new AccountInfo();
			accInfo.setFullname(username);
			acc.setAccountInfo(accInfo);
			accInfo.setAccount(acc);
			session.save(acc);
			// tao cart cho tai khoan
			Cart cart = new Cart();
			cart.setAccount(acc);
			cart.setIsdeleted(0);
			cart.setTotal(0.0f);
			session.save(cart);
			return acc;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bestbuy.dao.AccountDao#RegisterAccountWithFacebook(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String, java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public Account RegisterAccountWithFacebook(String username,
			String fullname, String gender, String linkFacebook) 
	{
		Session session = sessionFactory.getCurrentSession();
		// kiem tra xem user nay co ton tai chua
		String hql = "select p from Account p where p.username = ?";
		Query query = session.createQuery(hql);
		query.setString(0, username);
		Account account = (Account) query.uniqueResult();
		if (account != null) // user nay` da ton tai
		{
			return account;
		} else {
			// khoi tao account
			// luu account lai
			hql = "from AccountRole where role = ?";
			query = session.createQuery(hql);
			query.setString(0, "User");
			AccountRole role = (AccountRole) query.uniqueResult();
			// Tao acc
			Account acc = new Account();
			acc.setUsername(username);
			acc.setIsdeleted(0);
			acc.setAccountRole(role);
			role.getAccounts().add(acc);

			// tao info cho account nay
			AccountInfo accInfo = new AccountInfo();
			accInfo.setFullname(fullname);
			accInfo.setSex(gender);
			accInfo.setLinkFacebook(linkFacebook);
			acc.setAccountInfo(accInfo);
			// get wa account
			accInfo.setAccount(acc);
			session.save(acc);
			// tao cart cho tai khoan
			Cart cart = new Cart();
			cart.setAccount(acc);
			cart.setIsdeleted(0);
			cart.setTotal(0.0f);
			session.save(cart);
			return acc;
		}
	}
	
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.AccountDao#UpdateAccountInfo(java.lang.String)
	 */
	public Account UpdateAccountInfo(String username, String fullname,
			String sex, String birthday, String address, String email,
			String phone) {
		Session session = sessionFactory.getCurrentSession();
		Account account = GetAccount(username);
		// lay ra thong tin cua account nay`
		String hql = "select p from AccountInfo p where p.id = ?";
		Query query = session.createQuery(hql);
		query.setLong(0, account.getIdaccount());
		AccountInfo accInfo = (AccountInfo) query.uniqueResult();
		// cap nhat thong tin account nay`
		if (fullname.equals("") == false) {
			accInfo.setFullname(fullname);
		}
		if (sex.equals("") == false) {
			accInfo.setSex(sex);
		}
		if (birthday.equals("") == false) {
			accInfo.setBirthday(birthday);
		}
		if (address.equals("") == false) {
			accInfo.setAddress(address);
		}
		if (email.equals("") == false) {
			accInfo.setEmail(email);
		}
		if (phone.equals("") == false) {
			accInfo.setPhone(phone);
		}
		// save lai
		session.update(accInfo);
		return account;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.AccountDao#GetAccoutInfo(java.lang.String)
	 */
	public AccountInfo GetAccoutInfo(String username) {

		Session session = sessionFactory.getCurrentSession();
		String sql = "select accInfo from AccountInfo accInfo where accInfo.account.username = ?";
		Query query = session.createQuery(sql);
		query.setString(0, username);
		AccountInfo accountInfo = (AccountInfo) query.uniqueResult();
		if (accountInfo == null) // user nay` chua ton tai
		{
			return null;
		} else {

			return accountInfo;
		}
	}

	/*
	 * author ThanhNgo
	 */
	public Account GetAccount(String username) {

		Session session = sessionFactory.getCurrentSession();
		String sql = "select user from Account user where username = ?";
		Query query = session.createQuery(sql);
		query.setString(0, username);
		Account account = (Account) query.uniqueResult();
		if (account == null) // user nay` chua ton tai
		{
			return null;
		} else {

			return account;
		}
	}

	/*
	 * author ThanhNgo
	 */
	public Account Authenticate(String username, String password) {

		String passMD5 = AccountUtil.md5(password);
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Account where username = ? and password = ? and isdeleted = ?";
		Query query = session.createQuery(sql);
		query.setString(0, username);
		query.setString(1, passMD5);
		query.setInteger(2, 0);
		Account account = (Account) query.uniqueResult();
		if (account != null) {
			return account;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.AccountDao#updateAccount(com.bestbuy.pojo.Account)
	 */
	public void updateAccount(Account account) {
		Session session = sessionFactory.getCurrentSession();
		session.update(account);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bestbuy.dao.AccountDao#deleteAccount()
	 */
	public void deleteAccount(Account account) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(account);
	}

	// public static void main(String[] args)
	// {
	// AccountDao dao = new AccountDaoImpl();
	// Account account = new Account();
	// account = dao.RegisterAccount("hoangtu", "123");
	//
	// }
}
